package com.ruoyi.web.controller.vantController.customerController;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.AliyunSmsUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.zhuangxiumain.vo.CustomerVo;
import com.ruoyi.system.domain.zhuangxiumain.vo.PasswordVo;
import com.ruoyi.system.service.vantservice.customerervice.ApiCustomerVoService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by 小董 on 2021/3/10 16:13
 */
@RestController
@RequestMapping("/api/customer/vo")
@CrossOrigin
public class ApiCustomerVoController {
    @Autowired
    private ApiCustomerVoService apiCustomerVoService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查询用户信息
     * @return
     */
    @PostMapping("/check")
    public AjaxResult check(){
        return AjaxResult.success(apiCustomerVoService.selectCustomerVo());
    }

    /**
     * 修改用户性别
     * @param customerVo
     * @return
     */
    @PostMapping("/byCi")
    public AjaxResult byCi(@RequestBody CustomerVo customerVo){
        return AjaxResult.success(apiCustomerVoService.updateCustomerVoOne(customerVo));
    }
    /**
     * 获取验证码
     * @return
     */
    @PostMapping("/yan")
    public AjaxResult yan(){
        try {
            CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
            // 创建短信验证码类对象
            AliyunSmsUtils smsUtils = new AliyunSmsUtils();
            // 生成随机六位验证码
            smsUtils.setNewcode();
            // 获取验证码
            String code = Integer.toString(smsUtils.getNewcode());
            SendSmsResponse response = smsUtils.sendSms(customerLogin.getLoginName(), code);
            return AjaxResult.success(response);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error("发送失败");
    }

    /**
     * 注销用户信息
     * @param customerVo
     * @return
     */
    @PostMapping("/zhuXiao")
    public AjaxResult zhuXiao(@RequestBody CustomerVo customerVo){
        return AjaxResult.success(apiCustomerVoService.deleteCustomerVoOne(customerVo));
    }

    /**
     * 修改密码
     * @param passwordVo
     * @return
     */
    @PostMapping("/upPassword")
    public AjaxResult upPassword(@RequestBody PasswordVo passwordVo){
        return AjaxResult.success(apiCustomerVoService.updatePassword(passwordVo));
    }

    /**
     * 获取验证码
     * @param customerVo
     * @return
     */
    @PostMapping("/yan1")
    public AjaxResult yan1(@RequestBody CustomerVo customerVo){
        try {
            // 创建短信验证码类对象
            AliyunSmsUtils smsUtils = new AliyunSmsUtils();
            // 生成随机六位验证码
            smsUtils.setNewcode();
            // 获取验证码
            String code = Integer.toString(smsUtils.getNewcode());
            SendSmsResponse response = smsUtils.sendSms(String.valueOf(customerVo.getMobilePhone()), code);
            return AjaxResult.success(response);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error("发送失败");
    }

    /**
     * 修改用户绑定的手机号
     * @param customerVo
     * @return
     */
    @PostMapping("/moPhone")
    public AjaxResult moPhone(@RequestBody CustomerVo customerVo){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        customerVo.setCustomerId(customerLogin.getCustomerId());
        return AjaxResult.success(apiCustomerVoService.updateCustomerVoOne(customerVo));
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadImageOne")
    public AjaxResult uploadImageOne(@RequestParam("file") MultipartFile file) throws IOException
    {
        System.out.println(file);
        if (!file.isEmpty()){
            String avatar = FileUploadUtils.upload(RuoYiConfig.getRentalPath(), file);
            return AjaxResult.success("上传成功",avatar);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

}
